/*
 * DTDSimpleRule.cpp
 *
 *  Created on: 29 mars 2011
 *      Author: gdoussin
 */

#include <string>
#include "DTDSimpleRule.h"
#include "../commun.h"
#include <iostream>

using namespace std;

DTDSimpleRule::DTDSimpleRule(string name, Tcardinality cardinality):DTDRule(cardinality)
{
	this->name = name;
}

bool DTDSimpleRule::isValid(vector< string > balises)
{
	if(balises[0].compare(this->name) == 0)
	{
		if(balises.size() > 1)
		{
			string val = balises[0];
			bool coherent = true;
			vector< string >::iterator it;
			// on vérifie que le vecteur contienne la même balise
			for(it = balises.begin(); it != balises.end(); it++)
			{
				if(val.compare(*it) != 0) coherent = false;
			}
			if(coherent && (cardinality == CARD_PLUS || cardinality == CARD_STAR))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return true;
		}
	}
	else
	{
		cerr << "La balise " << balises[0] << " ne se trouve pas à la bonne place." << endl;
		return false;
	}
}

string DTDSimpleRule::getName()
{
	return name;
}

string DTDSimpleRule::toString()
{
	string s = name;

	switch (cardinality)
	{
		case CARD_PLUS :
			s += "+";
			break;

		case CARD_QMARK :
			s += "?";
			break;

		case CARD_STAR :
			s += "*";
			break;

		case CARD_NONE :
			break;

		default:
			break;
	}

	return s;
}
